package com.example.photograph.service.impl;

import com.example.photograph.domain.*;
import com.example.photograph.mapper.SysDepartmentMapper;
import com.example.photograph.mapper.SysUserDepMapper;
import com.example.photograph.service.ISysUserDepService;
import com.example.photograph.utils.Convert;
import com.example.photograph.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户和部门关联Service业务层处理
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Service
public class SysUserDepServiceImpl implements ISysUserDepService 
{
    @Autowired
    private SysUserDepMapper sysUserDepMapper;
    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;

    /**
     * 查询用户和部门关联
     * 
     * @param userId 用户和部门关联ID
     * @return 用户和部门关联
     */
    @Override
    public SysUserDep selectSysUserDepById(Long userId)
    {
        return sysUserDepMapper.selectSysUserDepById(userId);
    }

    /**
     * 查询用户和部门关联列表
     * 
     * @param sysUserDep 用户和部门关联
     * @return 用户和部门关联
     */
    @Override
    public List<SysUserDep> selectSysUserDepList(SysUserDep sysUserDep)
    {
        return sysUserDepMapper.selectSysUserDepList(sysUserDep);
    }

    /**
     * 新增用户和部门关联
     * 
     * @param sysUserDep 用户和部门关联
     * @return 结果
     */
    @Override
    public int insertSysUserDep(SysUserDep sysUserDep)
    {
        return sysUserDepMapper.insertSysUserDep(sysUserDep);
    }

    /**
     * 修改用户和部门关联
     * 
     * @param sysUserDep 用户和部门关联
     * @return 结果
     */
    @Override
    public int updateSysUserDep(SysUserDep sysUserDep)
    {
        return sysUserDepMapper.updateSysUserDep(sysUserDep);
    }

    /**
     * 删除用户和部门关联对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysUserDepByIds(String ids)
    {
        return sysUserDepMapper.deleteSysUserDepByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户和部门关联信息
     * 
     * @param userId 用户和部门关联ID
     * @return 结果
     */
    @Override
    public int deleteSysUserDepById(Long userId)
    {
        return sysUserDepMapper.deleteSysUserDepById(userId);
    }

    /**
     * 批量新增用户部门信息
     *
     * @param departmentId 部门ID
     * @param userIds 用户数据ID
     * @return 结果
     */
    @Override
    public int insertAuthUsers(Long departmentId, String userIds)
    {
        Long[] users = Convert.toLongArray(userIds);
        // 新增用户与角色管理
        List<SysUserDep> list = new ArrayList<SysUserDep>();
        for (Long userId : users)
        {
            SysUserDep ud = new SysUserDep();
            ud.setUserId(userId);
            ud.setDepartmentId(departmentId);
            list.add(ud);
        }
        return sysUserDepMapper.batchUserDep(list);
    }

    /**
     * 根据订单ID查询员工
     *
     * @param orderId 订单Id
     * @return 员工列表
     */
    @Override
    public List<Ztree> userDepTreeData(Long orderId)
    {
        List<SysUserDep> userDepList = sysUserDepMapper.selectUserDepAll();
        List<String> userList = sysUserDepMapper.selectUserDepByOrderId(orderId);
        List<SysDepartment> departmentListList = sysDepartmentMapper.selectSysDepartmentList(new SysDepartment());
        return initZtree(departmentListList,userDepList, userList);
    }

    /**
     * 对象转菜单树
     *
     * @param userDepList 用户列表
     * @param userList 已存在用户列表
     * @return 树结构列表
     */
    private List<Ztree> initZtree(List<SysDepartment> departmentListList, List<SysUserDep> userDepList, List<String> userList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(userList);
        for (SysDepartment department : departmentListList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(department.getDepartmentId()+10000);
            ztree.setpId(0L);
            ztree.setName(department.getDepartmentName());
            ztree.setTitle(department.getDepartmentName());
            ztrees.add(ztree);
        }
        for (SysUserDep sysUserDep : userDepList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(sysUserDep.getUserId());
            ztree.setpId(sysUserDep.getDepartmentId()+ 10000);
            ztree.setName(sysUserDep.getUserName());
            ztree.setTitle(sysUserDep.getDepartmentName());
            if (isCheck)
            {
                ztree.setChecked(userList.contains(sysUserDep.getUserId().toString()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
